package com.cxf.windex.rest;

import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.msg.TableResultResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.base.util.CutHtml;
import com.cxf.windex.biz.WeiContentBiz;
import com.cxf.windex.dto.Types;
import com.cxf.windex.entity.WeiContent;
import com.cxf.windex.entity.WeiType;
import com.cxf.windex.task.TypeTask;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @author chenxf
 * @date 4/20/2018
 * @description
 */
@Controller
@RequestMapping("content")
public class WeiContentController extends BaseRestController<WeiContentBiz, WeiContent> {

  @Autowired
  private TypeTask typeTask;
  Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");

  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ResponseBody
  public TableResultResponse<WeiContent> list(
      @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
      String name) {

    Example example = new Example(WeiType.class);
    Criteria criteria = example.createCriteria();
    if (StringUtils.isNotBlank(name)) {
      criteria.
          andLike("name", "%" + name + "%");
    }

    example.setOrderByClause("crt_time desc ");
    PageHelper.startPage(offset, limit);
    List<WeiContent> items = baseBiz.selectByExample(example);

    PageInfo<WeiContent> pageInfo = new PageInfo<WeiContent>(items);
    return new TableResultResponse<WeiContent>(Integer.parseInt(String.valueOf(pageInfo.getTotal())),
        offset, limit,
        items);
  }

  @RequestMapping(value = "/view/{id}",method = RequestMethod.GET)
  public String view(Map<String,Object> map,@PathVariable Long id){
    WeiContent content=baseBiz.selectById(id);
    initSeo(map,content.getTitle(),content.getTag(),content.getIntro());
    map.put("article",content);
    return this.render("article");
  }

  @ApiOperation(value = "新增", notes = "新增")
  @RequestMapping(value = "/edit", method = RequestMethod.GET)
  public String edit(WeiContent entity) {

    return "admin/article_add";
  }

  @ApiOperation(value = "保存", notes = "保存")
  @RequestMapping(value = "/edit", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse save(WeiContent entity) {
    if (StringUtils.isNotBlank(entity.getContent()) &&
        entity.getContent().contains("img")) {
      entity.setImage(getImg(entity.getContent()));
    }
    typeTask.doTypeTask(entity.getType().split(","), Types.TAG);
    typeTask.doTypeTask(entity.getType().split(","), Types.TYPE);
    //entity.setIntro(CutHtml.cutPrettyHtmlString(entity.getContent(),50,true));
    baseBiz.insert(entity);
    return new ObjectRestResponse().rel(true).msg("保存成功");
  }

  @ApiOperation(value = "上传图片", notes = "上传图片")
  @RequestMapping(value = "/upload", method = RequestMethod.POST)
  @ResponseBody
  public Map upload(@RequestParam("file") MultipartFile file) {
    Map resultMap = new HashMap();
    if (file != null) {
      Map<String, String> savePcMap = saveFile(request, file, "article");
      if (savePcMap != null) {
        if (savePcMap.get("code").equals("0")) {
          Map fileMap = new HashMap();
          fileMap.put("src", savePcMap.get("saveUrl"));
          resultMap.putAll(savePcMap);
          resultMap.put("data", fileMap);
          return resultMap;
        } else {
          return savePcMap;
        }
      }
    }
    return resultMap;
  }

  public String getImg(String content) {

    Matcher m = p.matcher(content);
    if (m.find()) {
      return m.group(1);
    }
    return "";
  }
}
